package mwt.xml.xdbforms.xforms.impl;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import mwt.xml.xdbforms.dbms.exceptions.TypeNotFoundException;
import mwt.xml.xdbforms.dbms.metadata.impl.ColumnMetadata;
import mwt.xml.xdbforms.dbms.metadata.impl.TableMetadata;
import mwt.xml.xdbforms.xforms.UIHandler;
import mwt.xml.xdbforms.xforms.XFormsUIManager;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;

public class XFormsUIManagerImpl implements XFormsUIManager {
    private Document document;

    public XFormsUIManagerImpl() throws ParserConfigurationException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(false);
        dbf.setValidating(false);

        DocumentBuilder db = dbf.newDocumentBuilder();

        this.document = db.newDocument();
    }

    public Document getUI(TableMetadata table) throws TypeNotFoundException {
        this.buildDocument(table);

        return this.document;
    }

    public String getUIAsString(TableMetadata table) throws TypeNotFoundException {
       this.buildDocument(table);

       return this.printDocument();
    }

    private void buildDocument(TableMetadata table) throws TypeNotFoundException {
        UIHandler uih = new MySQLUIHandler();
        Element group = this.document.createElement("xf:group");

        for (ColumnMetadata col : table.getColumns()) {
            if ("NO".equals(col.getIsAutoincrement())) {
                Element input = this.document.createElement(uih.getUIElement(col.getTypeName()));
                input.setAttribute("ref", col.getName());

                Node label = this.document.createElement("xf:label");
                label.setTextContent(col.getName());
                input.appendChild(label);

                /*Node alert = this.document.createElement("xf:alert");
                alert.setTextContent("Il campo "+col.getName()+" contiene dati non validi");
                input.appendChild(alert);*/

                group.appendChild(input);
            }
        }

        Element submit = this.document.createElement("xf:submit");
        submit.setAttribute("submission", table.getName());
        group.appendChild(submit);

        Node label = this.document.createElement("xf:label");
        label.setTextContent("Salva");
        submit.appendChild(label);

        this.document.appendChild(group);
    }

    private String printDocument() {
        DOMImplementationLS ls = (DOMImplementationLS) this.document.getImplementation();
        LSOutput lso = ls.createLSOutput();
        LSSerializer lss = ls.createLSSerializer();
        //StringWriter sw = new StringWriter();

        lso.setEncoding("ISO-8859-1");
        //lso.setCharacterStream(sw);
        //lss.getDomConfig().setParameter("normalize-character", false);
        lss.getDomConfig().setParameter("format-pretty-print", true);
        lss.getDomConfig().setParameter("xml-declaration", false);

        //lss.write(this.document, lso);
        return lss.writeToString(document);
        //return sw.toString();
    }
}
